/**
 * 
 */
package Groep3;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;





/**
 * @author Frederike
 *
 */
public class P2Pserver {

	private ServerSocket idleSocket;
	private Socket connectionSocket;
	private int sequenceNumber;
	private DataOutputStream outToOther;
	private BufferedReader inFromOther;
	private String type;
	private int acknumber;
	private String[] buffer;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		// TODO Auto-generated method stub
		try {
			new P2Pserver();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public P2Pserver(){
		sequenceNumber = 0;
		acknumber = 0;
		//buffer = new ArrayList<String>();
		Receiving();
		
		Sending();
	}
	
	public void Receiving() {		
		try
		{
			idleSocket = new ServerSocket(6789);
			System.out.println("server started");
		}
		catch(IOException e)
		{
			//System.out.println("Kan server niet starten");
			return;
		}
		
		while( true )
		{
			try
			{
				System.out.println("startReceiving!");
				Thread thread = new Thread(new P2Pclient(idleSocket));
				
				thread.start();
			}
			catch(Exception e)
			{
				System.out.println("Fatale error: " + e);
				System.exit(0);
			}finally{
			}
		}
	}
	
	public void Sending() {
		try {
			connectionSocket = new Socket("localhost", 6789);
			
			System.out.println("start Sending!");
			
			outToOther = new DataOutputStream(connectionSocket.getOutputStream());
			inFromOther = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
			BufferedReader inFromServer = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
			
			while (true) {
				//Timer timer = new Timer();
				String sendThis;
				Scanner in = new Scanner(System.in);
				System.out.println("Voer bericht in:");
				sendThis = in.nextLine();
				String sn = Integer.toString(sequenceNumber);
				String an = Integer.toString(acknumber);


				try {
					if (!connectionSocket.isClosed()){
						type = "d";
						outToOther.writeBytes(new String(type +" " + sn +" "+ an +" "+ sendThis )+'\n');
						sequenceNumber++;
						acknumber++;
						//timer.schedule(submit(), 5000);
						String message = null;
						//outToOther.close();
						try{
							System.out.println(connectionSocket.isClosed());
							
							if((message = inFromServer.readLine()) != null){
								
								System.out.println(message);		
							}
							//inFromServer.close();
						}catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
						}
						
					}
						
					
					
					//inFromOther.readLine();
				} catch (IOException e) {
					System.out.println("Write socket error: " + e);
					connectionSocket.close();
				}

				
				//System.out.println(command);
				//return;
			}
			
		} catch (Exception e1) {
			e1.printStackTrace();
			System.exit(0);
		}
	}

	private TimerTask submit() {
		// TODO Auto-generated method stub
		//outToOther.writeBytes(new String(type +" " + sn +" "+ an +" "+ sendThis ) + "\n\r");
		try {
			outToOther.writeBytes(new String("opnieuw versturen"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
